package com.tinno.chargelauncher.application;

import android.app.Application;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.util.Log;

import com.tinno.chargelauncher.service.ChargeListenerService;

public class ChargeLauncherApplication extends Application {

	private static final String TAG = ChargeLauncherApplication.class.getSimpleName();

	private static Context chargeLauncherApplication = null;

	@Override
	public void onCreate() {
		super.onCreate();
		chargeLauncherApplication = this;
	}

	@Override
	public void onTerminate() {
		super.onTerminate();
		unbindService(serviceConnection);
	}

	public static void bindService(Context context){
		if (chargeLauncherApplication == null || context == null){
			Log.d(TAG, "bindService: chargeLauncherApplication null!!");
			if (context == null){
				Log.i(TAG, "bindService: context null!!");
				return;
			}
			chargeLauncherApplication = context.getApplicationContext();
		}
		Intent intent = new Intent(chargeLauncherApplication, ChargeListenerService.class);
		chargeLauncherApplication.bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
	}

	private static ServiceConnection serviceConnection = new ServiceConnection() {

		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			Log.d(TAG, "ServiceConnection: onServiceConnected: ");
		}

		@Override
		public void onServiceDisconnected(ComponentName name) {
			Log.d(TAG, "ServiceConnection: onServiceDisconnected: ");
		}

		@Override
		public void onNullBinding(ComponentName name) {
			ServiceConnection.super.onNullBinding(name);
			Log.d(TAG, "ServiceConnection: onNullBinding: ");
		}
	};
}
